<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use App\Models\AttributeGroup;
use Encore\Admin\Controllers\AdminController;

class AttributeGroupsController extends AdminController
{
    protected $title = '商品属性管理';
    
    protected function grid()
    {
        $grid = new Grid(new AttributeGroup());

        $grid->column('id', 'ID');
        $grid->column('title', '属性组名');
        $grid->column('created_at', '创建')->display(function () {
        	return $this->created_at->toDatetimeString();
        });

        return $grid;
    }
    
    protected function form()
    {
        $form = new Form(new AttributeGroup());
	    
        $form->text('title', '属性组名')->rules('required|unique:attribute_groups,title,{{id}}');
        
        $form->hasMany('attributekeys', '组内属性名', function (Form\NestedForm $form) {
	        $form->text('key', '属性名')->rules('required');
        });

        return $form;
    }
}
